package com.mall.cart.controller;

import com.mall.cart.model.Cart;
import com.mall.cart.service.ICartService;
import com.mall.common.util.RespResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Darren
 * @date 2022-08-29 10:00
 * Description：
 */
@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {
    @Autowired
    private ICartService cartService;

    @GetMapping("/{id}/{num}")
    @ApiOperation(value = "新增")
    public RespResult addCart(@PathVariable("id") String id, @PathVariable("num") Integer num) {
        String username = "gp";
        cartService.addCart(id, username, num);
        return RespResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation(value = "根据用户名称查询购物车列表")
    public RespResult<List<Cart>> list() {
        String userName = "gp";
        return RespResult.ok(cartService.list(userName));
    }

    @PostMapping("/list")
    @ApiOperation(value = "购物车结算")
    public RespResult<List<Cart>> list(@RequestBody List<String> ids) {
        return RespResult.ok(cartService.list(ids));
    }

    @DeleteMapping("/list")
    @ApiOperation(value = "购物车删除")
    public RespResult deleteCart(@RequestBody List<String> ids) {
        cartService.deleteCart(ids);
        return RespResult.ok();
    }
}
